package cn.adelyn.blog.blogmanager.controller;

import cn.adelyn.base.api.snowfalke.feign.SnowflakeFeignClient;
import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.blogmanager.constant.BlogConstant;
import cn.adelyn.blog.blogmanager.pojo.bo.InsertBlogDecryptBO;
import cn.adelyn.blog.blogmanager.pojo.dto.BlogSearchPageDTO;
import cn.adelyn.blog.blogmanager.pojo.dto.InsertBlogDTO;
import cn.adelyn.blog.blogmanager.pojo.vo.BlogVO;
import cn.adelyn.blog.blogmanager.service.BlogService;
import cn.adelyn.common.core.cglib.CglibUtil;
import cn.adelyn.common.core.pojo.vo.PageVO;
import cn.adelyn.common.core.response.ServerResponseEntity;
import cn.adelyn.common.crypto.utils.URLAesEncodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Validated
@RestController
@RequestMapping("/artical")
public class BlogController {

    @Autowired
    SnowflakeFeignClient snowflakeFeignClient;

    @Autowired
    BlogService blogService;

    @GetMapping()
    public ServerResponseEntity<BlogVO> getBlogInfo(@NotNull(message = "blogId 不能为空~") Long blogId) {
        return ServerResponseEntity.success(blogService.getBlogInfo(blogId));
    }

    @PostMapping("/page")
    public ServerResponseEntity<PageVO<BlogVO>> getBlogPage(@RequestBody @Valid BlogSearchPageDTO blogSearchPageDTO) {
        blogSearchPageDTO.setBlogVisible(BlogConstant.VISIBLE_PUBLIC);
        return ServerResponseEntity.success(blogService.getBlogPage(blogSearchPageDTO, blogSearchPageDTO.getPageDTO()));
    }

    @PostMapping("/userPage")
    public ServerResponseEntity<PageVO<BlogVO>> getUserBlogPage(@RequestBody @Valid BlogSearchPageDTO blogSearchPageDTO) {
        blogSearchPageDTO.setAuthorId(AuthUserContext.get().getUserId());
        return ServerResponseEntity.success(blogService.getBlogPage(blogSearchPageDTO, blogSearchPageDTO.getPageDTO()));
    }

    @PostMapping
    public ServerResponseEntity insertBlog(@RequestBody @Valid InsertBlogDTO insertBlogDTO) {
        insertBlogDTO.setAuthorId(AuthUserContext.get().getUserId());
        insertBlogDTO.setBlogId(snowflakeFeignClient.snowflakeId().getData());

        InsertBlogDecryptBO insertBlogDecryptBO = parsePicIdList(insertBlogDTO);

        blogService.insertBlog(insertBlogDecryptBO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    public ServerResponseEntity updateBlog(@RequestBody @Valid InsertBlogDTO insertBlogDTO) {
        insertBlogDTO.setAuthorId(AuthUserContext.get().getUserId());

        InsertBlogDecryptBO insertBlogDecryptBO = parsePicIdList(insertBlogDTO);

        blogService.updateBlog(insertBlogDecryptBO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    public ServerResponseEntity deleteBlog(@NotNull(message = "blogId 不能为空~") Long blogId) {
        blogService.deleteBlog(blogId);
        return ServerResponseEntity.success();
    }

    /**
     * 解析 url编码的 加密的 picId
     */
    private InsertBlogDecryptBO parsePicIdList(InsertBlogDTO insertBlogDTO) {
        InsertBlogDecryptBO insertBlogDecryptBO = CglibUtil.copy(insertBlogDTO, InsertBlogDecryptBO.class);
        if (Objects.isNull(insertBlogDTO.getPicIdList())) {
            return insertBlogDecryptBO;
        }

        List<Long> picIdList = insertBlogDTO.getPicIdList().stream()
                .map(picId -> {
                    String deEncrypyPicId = URLAesEncodeUtil.encryptDecode(picId);
                    return Long.parseLong(deEncrypyPicId);
                })
                .collect(Collectors.toList());
        insertBlogDecryptBO.setPicIdList(picIdList);

        return insertBlogDecryptBO;
    }
}
